package com.crm.crm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.crm.crm.domain.dto.SysRoleDto;
import com.crm.crm.domain.dto.UserPageQueryParam;
import com.crm.crm.domain.role.SysRole;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.domain.vo.TreeSelectVo;
import com.crm.crm.domain.vo.UserVo;
import com.crm.crm.domain.vo.business.result.Result;
import com.crm.crm.mapper.UserRoleMapper;
import com.crm.crm.service.SystemService;
import com.crm.crm.service.UserRoleService;
import com.crm.crm.service.UserService;
import com.crm.crm.service.impl.SystemServiceImpl;
import com.crm.crm.util.UserThreadLocalUtil;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/role")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private SystemService systemService;
    //分页查询
    @GetMapping("/list")
    public Result PageQuery(UserPageQueryParam userPageQueryParams, @RequestParam Map<String,String> params){
        userPageQueryParams.setBeginTime(params.get("params[beginTime]"));
        userPageQueryParams.setEndTime(params.get("params[endTime]"));
        PageResult<SysRole> pageResult = userRoleService.pageQuery(userPageQueryParams);
        return Result.success().put("total",pageResult.getTotal()).put("rows",pageResult.getRows());
    }
    //新增
    @PostMapping
    public Result add(@RequestBody SysRoleDto sysRole){
        sysRole.setCreateTime(LocalDateTime.now());
        sysRole.setUpdateTime(LocalDateTime.now());
        sysRole.setCreateBy(UserThreadLocalUtil.getCurrentUserName());
        sysRole.setUpdateBy(UserThreadLocalUtil.getCurrentUserName());
        sysRole.setDelFlag("0");
        userRoleService.save(sysRole);
        Long roleId = sysRole.getRoleId();
        List<Long> menuIds = sysRole.getMenuIds();
        userRoleService.saveRoleMenuIds(roleId,menuIds);
        return Result.success();
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id){
        SysRole sysRole = userRoleMapper.selectById(id);
        SysRoleDto sysRoleDto = new SysRoleDto();
        BeanUtil.copyProperties(sysRole,sysRoleDto);
        sysRoleDto.setMenuIds(userRoleMapper.selectMenuIdsByRoleId(id));
        return Result.success().put("data",sysRoleDto);
    }
    //修改角色
    @PutMapping
    public Result update(@RequestBody SysRoleDto sysRole){
        sysRole.setUpdateTime(LocalDateTime.now());
        sysRole.setUpdateBy(UserThreadLocalUtil.getCurrentUserName());
        userRoleService.updateById(sysRole);
        Long roleId = sysRole.getRoleId();
        List<Long> menuIds = sysRole.getMenuIds();
        userRoleMapper.deleteRoleMenuByRoleId(roleId);
        userRoleService.saveRoleMenuIds(roleId,menuIds);
        return Result.success();
    }
    //删除
    @DeleteMapping("/{ids}")
    @Transactional
    public Result delete(@PathVariable("ids") List<Long> ids){
        userRoleService.removeBatchByIds(ids);
        for (Long id : ids) {
            userRoleMapper.deleteRoleMenuByRoleId(id);
        }
        return Result.success();
    }

}
